package top.chendawei.main.web.controller;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.chendawei.system.beans.entity.OperationLog;
import top.chendawei.system.beans.vo.param.OperationLogParam;
import top.chendawei.system.enums.CommomOperator;
import top.chendawei.system.service.ICommonService;
import top.chendawei.system.util.annotation.SysLog;
import top.chendawei.system.web.controller.AbstractBaseController;
import top.chendawei.util.JsonResult;
import top.chendawei.util.params.ResultMessage;

import java.util.ArrayList;
import java.util.List;

@RestController
@Controller
@RequestMapping("/sys/log")
@Api(tags = "操作日志")
public class SysLogController extends AbstractBaseController {
    private static final long serialVersionUID = -8971852540641555704L;
    @Autowired
    private ICommonService commonService;

    @SysLog(description = "查询操作日志")
    @GetMapping({"/page"})
    public ResultMessage page(OperationLogParam param) {
        initPageParam(param);
        List<String> searchAttr = new ArrayList();
        List<Object> searchVal = new ArrayList();
        List<CommomOperator> searchOper = new ArrayList();
        if (StringUtils.isNoneBlank(param.getStartTime())) {
            searchAttr.add("updateDate");
            searchVal.add(param.getStartTime());
            searchOper.add(CommomOperator.ge);
        }
        if (StringUtils.isNoneBlank(param.getEndTime())) {
            searchAttr.add("updateDate");
            searchVal.add(param.getEndTime());
            searchOper.add(CommomOperator.le);
        }
        if (StringUtils.isBlank(param.getOrderBy())) {
            param.setOrderBy("updateDate");
            param.setOrderType("desc");
        }
        JsonResult<OperationLog> json = this.commonService.search(searchAttr, searchVal, searchOper,
                OperationLog.class, param.getCurrent().intValue(), param.getPageSize().intValue(), param.getOrderBy(), param.getOrderType());
        return ResultMessage.success(json);
    }

}
